/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Binhluan;
import pojo.Danhsachsanpham;
import pojo.Loaisanpham;
import pojo.Nguoidung;
import pojo.Sanpham;
import until.HibernateUtil;

/**
 *
 * @author Alvis
 */
public class BinhLuanDao {
    private Session session;

    public BinhLuanDao() {
        session = HibernateUtil.getSessionFactory().openSession();

    }
    public void Update(Binhluan value, int id) {
        session.getTransaction().begin();
        Binhluan valueTemp = (Binhluan) session.get(Binhluan.class, id);
        valueTemp.setNoidung(value.getNoidung());
        session.update(valueTemp);
        // session.flush();
        session.getTransaction().commit();
    }
    public void Delete(int id) {
        session.getTransaction().begin();
        String sql = "from Binhluan where mabinhluan=?";
        Query query = session.createQuery(sql);
        query.setInteger(0,id);
        List result = query.list();
        Binhluan[] listBinhLuan = new Binhluan[result.size()];
        result.toArray(listBinhLuan);
        session.delete(session.get(Binhluan.class, id));
        session.flush();
        session.getTransaction().commit();
    }
     public Binhluan[] getAllComment() {
        session.getTransaction().begin();
        String sql = "from Binhluan";
        Query query = session.createQuery(sql);
        List result = query.list();
        Binhluan[] listvalue = new Binhluan[result.size()];
        result.toArray(listvalue);
        return listvalue;
    }
    public Binhluan getComment(int id) {

        session.getTransaction().begin();
        String sql = "from Binhluan where mabinhluan=?";
        Query query = session.createQuery(sql);

        query.setInteger(0,id);
        Binhluan result = (Binhluan) query.uniqueResult();
        session.flush();
        session.getTransaction().commit();
        return result;

    }

    
}
